#include "fig_rocket_topido.h"
#include "fig_enemy_manager.h"
#include "fig_life_obj.h"
#include "fig_level.h"
#include "fighter.h"
#include "fig_sfx.h"

#include "math/facing.h"
#include "num/rand.h"

FIGRocketTopido::FIGRocketTopido(float DamagePower)   : 
									FIGBulletRocket(DamagePower,false)
{
	 m_Acceleration = 0.5f;
	 m_RotationSpeed = 100.0f;
	 m_Type = FIGBullet::BULLET_TYPE_PLAYER_TOPIDO;
	 	
}


void					FIGRocketTopido::Render(const math::Vec2& Offset,float Scale)
{
	FIGBulletRocket::Render(Offset,Scale);
}
void					FIGRocketTopido::RenderDebug(const math::Vec2& Offset,float Scale)
{
	FIGBulletRocket::Render(Offset,Scale);
}
	
void					FIGRocketTopido::Tick(float dt)
{
	if(!m_IsAlive)
	{
		if(m_pSFX)
		{
			m_pSFX->ToDie();
			m_pSFX = NULL;
		}
		return;
	}

	if(m_pTarget && 
		m_pTarget->IsAlive() && 
		m_pTarget->IsInSideScreen() &&
		m_pTarget->IsHitAble() 
		)
	{
		
		if(m_Rotation < 0)
			m_Rotation += 360;
		
		if(m_Rotation > 360)
			m_Rotation -= 360;
		

		math::Vec2 TargetPos = m_pTarget->GetPos();
		math::Vec2 Dir = (TargetPos - m_Pos );
		if(m_RandomSwayDegree > 0 )
		{
			// too near?
			if(Dir.Magnitude() < 0.2f)
			{
				m_RandomSwayDegree = 0;
				m_SwayCount = 0;
				m_RotationSpeed = 360;
			}

			m_RandomSwayDegree -= m_RotationSpeed * dt;
			if(m_bSwayPlus)
				m_Rotation += m_RotationSpeed * dt;
			else
				m_Rotation -= m_RotationSpeed * dt;
		

			if(m_RandomSwayDegree <= 0)
			{
				m_SwayCount--;
				if(m_SwayCount > 0)
				{
					m_RandomSwayDegree = (float)num::RAND32::Instance().RandInt(25,45);
					m_bSwayPlus = !m_LastSway;
					m_LastSway = m_bSwayPlus;
				}
				
			}
		}
		else
		{
			m_RotationSpeed += (360 * dt);
			if(m_RotationSpeed > 360)
				m_RotationSpeed = 360;

			
			Dir.Normalize();
	
			float Degree = math::Facing::AngleVector(Dir);
			float Diff = Degree - m_Rotation;
			if(Diff < 0)
			{
				Diff *= -1;
				if(Diff < 180)
					m_Rotation -= m_RotationSpeed * dt;   				
				else
					m_Rotation += m_RotationSpeed * dt;
				
			}
			else
			{
				if(Diff < 180)
					m_Rotation += m_RotationSpeed * dt;				
				else
					m_Rotation -= m_RotationSpeed * dt;
				
			}
		}

		if(m_Rotation < 0)
			m_Rotation += 360;
		

		if(m_Rotation > 360)
			m_Rotation -= 360; 		


		m_Dir = math::Facing::VectorFromAngle(m_Rotation);
			
		
		
		
	}
	else
	{
		if(num::RAND32::Instance().RandInt(0,1) == 1)
		{
			// enemy first
			m_pTarget = FIGEnemyManager::Instance().GetTopidoTarget(this);		
			if(!m_pTarget)
				Fighter::Instance().GetLevel()->GetTopidoTarget(this);
		}
		else
		{
			// ground obj first
			m_pTarget = Fighter::Instance().GetLevel()->GetTopidoTarget(this);
			if(!m_pTarget)
				FIGEnemyManager::Instance().GetTopidoTarget(this);			
		}
	}
	if(!m_pTarget)
	{
		math::Vec2 Dir = math::Facing::VectorFromAngle(m_Rotation);
		m_Dir = Dir;
	}

	float MAXSPEED = 0.8f;
	m_Speed = m_Speed + ( m_Acceleration * dt);
	if(m_Speed > MAXSPEED)
		m_Speed = MAXSPEED;
		
	FIGBullet::Tick(dt);
	SFXTick(dt);
}

void					FIGRocketTopido::ReSpawn(const math::Vec2& Pos,
											FIGBullet::STARTPOS StartDir,
											const math::Vec2& TargetDir
											)
{

	
	m_pTarget = NULL;
	FIGBulletRocket::ReSpawn(Pos,StartDir,TargetDir);
	
	m_RandomSwayDegree = (float)num::RAND32::Instance().RandInt(15,45);
	if(num::RAND32::Instance().RandInt(0,1) == 1)
		m_bSwayPlus = true;
	else 
		m_bSwayPlus = false;

	m_LastSway = m_bSwayPlus;

	m_SwayCount = num::RAND32::Instance().RandInt(1,2);
	m_RotationSpeed = 100.0f;
	m_Speed = 0.3f;

}